﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace TooltracePC
{
    public partial class frmToolAdd : Form
    {
        dgv dgvObject = new dgv();

        public frmToolAdd()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void frmToolAdd_Load(object sender, EventArgs e)
        {
            int ToolID = dgvObject.getSelectID();

            if (ToolID > 0)
            {
                dbTool ToolDb = new dbTool();
                DataTable dtTool = null;
                dtTool = ToolDb.getByID(ToolID);
                btnOK.Text = "更新";
                this.Text = "更新记录";
                if(dtTool != null)
                {
                    tbLableID.Text = dtTool.Rows[0]["LableID"].ToString();
                    tbLableName.Text = dtTool.Rows[0]["LableName"].ToString();
                    tbLableNo.Text = dtTool.Rows[0]["LableNo"].ToString();
                }

            }
            else
            {
                btnOK.Text = "确定";
                this.Text = "添加记录";
            }



        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int ToolID = dgvObject.getSelectID();
            int i = 0;

            string LableID, LableName, LableNo;

            dbTool ToolDb = new dbTool();

            LableID = tbLableID.Text;
            LableName = tbLableName.Text;
            LableNo = tbLableNo.Text;

            if (string.IsNullOrEmpty(LableID) || string.IsNullOrEmpty(LableName) || string.IsNullOrEmpty(LableNo))
            {
                MessageBox.Show("必填项不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ToolID > 0)
            {
                if (ToolDb.updateDb(ToolID, LableID, LableName, LableNo) == 1)
                {
                    i = 1;
                }
            }
            else
            {
                if(ToolDb.insertDb(LableID, LableName, LableNo)  == 1)
                {
                    i = 1;           
                }
            }


            if(i == 1)
            {
                DataTable dtTool = null;
                dtTool = ToolDb.getAll();
                dgvObject.setDataSource(dtTool);

                //this.Hide();
                this.Close();
            }

        }

        private void btnC_Click(object sender, EventArgs e)
        {
            //this.Hide();
            this.Close();
        }
    }
}
